// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TransactionalBulbItem.cs" company="StyleCop for ReSharper Development Team">
//   Copyright (c) StyleCop for ReSharper Development Team. All rights reserved.
// </copyright>
// <summary>
//   Defines the TransactionalBulbItem type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace StyleCop.For.ReSharper.BulbItems.Framework
{
    #region Using Directives

    using JetBrains.ProjectModel;
    using JetBrains.ReSharper.Psi;
    using JetBrains.TextControl;

    #endregion

    /// <summary>
    /// Abstract Transactional Bulb Item for changes than need to be wrapped in a transaction.
    /// </summary>
    public abstract class TransactionalBulbItem : EnsureWritableBulbItem
    {
        #region Public Methods

        /// <summary>
        /// Wraps the EnsureWritable calls in a transaction. Used when making physical changes
        /// to the DOM.
        /// </summary>
        /// <param name="solution">
        /// The solution we belong to.
        /// </param>
        /// <param name="textControl">
        /// The control we started on.
        /// </param>
        public override void ExecuteEnsureWritable(ISolution solution, ITextControl textControl)
        {
            PsiManager.GetInstance(solution).DoTransaction(delegate { this.CallBaseExecuteEnsureWritable(solution, textControl); });
        }

        #endregion

        #region Methods

        /// <summary>
        /// Helper method required because if you try and call base.ExecuteEnsureWritable
        /// from within the delegate you get an assembly signing error.
        /// </summary>
        /// <param name="solution">
        /// The solution we belong to.
        /// </param>
        /// <param name="textControl">
        /// The control we started on.
        /// </param>
        private void CallBaseExecuteEnsureWritable(ISolution solution, ITextControl textControl)
        {
            base.ExecuteEnsureWritable(solution, textControl);
        }

        #endregion
    }
}